﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using RegistrationDate;

namespace RegisterTool
{
    /// <summary>
    /// 注册原理说明
    /// 1、软件启动时，会在系统的注册列表中根据时间生成一串密文，及注册标志也为密文，比较时间也为密文，共三串
    /// 2、当系统时间超过比较时间时，注册标志变为1，软件会弹出要求注册窗口，窗口上有机器码，注册码为空
    /// 3、将机器码复制到此注册软件上，进行注册码的生成，将生成的注册码复制到注册窗口进行注册
    /// </summary>
    public partial class FrmRegisterTool : Form
    {
        public FrmRegisterTool()
        {
            InitializeComponent();
        }

        private void FrmRegisterTool_Load(object sender, EventArgs e)
        {

        }

        private void btnReg_Click(object sender, EventArgs e)
        {
            string str = tbMachineCode.Text.Trim();
            if (str.Length == 0 || str.Length % 2 != 0)
            {
                MessageBox.Show("请输入正确的机器码");
                this.DialogResult = DialogResult.No;
                return;
            }

            string mCodeEn = HexStr2EnStr(str);
            string startTime = AESModel.AESDecrypt(mCodeEn);
            DateTime dt;
            try {
                dt = Convert.ToDateTime(startTime);
            } catch {
                MessageBox.Show("输入的机器码不正确！");
                return;
            }
            
            DialogResult dr = MessageBox.Show("启动时间：" + startTime + ",是否允许生成注册码？","提示：",MessageBoxButtons.YesNo);
            if (dr == DialogResult.No) return;

            if (cbPermanent.Checked == true) {
                dt = Convert.ToDateTime("2300-12-31 23:59:59");
            } else {
                dt = dtp.Value;
            }
            

            string endTime = dt.ToString("yyyy-MM-dd HH:mm:ss") + "." + startTime.Split('.')[1];

            TimeSpan ts = dt - DateTime.Now;
            if (ts.TotalDays < 365) {
                dr = MessageBox.Show("有效期少于一年,是否允许生成注册码？", "提示：", MessageBoxButtons.YesNo);
                if (dr == DialogResult.No) return;
            }

            string endTimeEn = AESModel.AESEncrypt(endTime);
            byte[] bytes = Encoding.ASCII.GetBytes(endTimeEn);
            string regStr = "";
            foreach (var b in bytes) {
                regStr += b.ToString("X2"); 
            }

            tbRegCode.Text = regStr;
        }

        private string HexStr2EnStr(string hexStr)
        {
            string[] strs = new string[hexStr.Length / 2];
            for (int i = 0; i < strs.Length; i++)
            {
                strs[i] = hexStr.Substring(i * 2, 2);
            }

            byte[] bytes = new byte[strs.Length];

            for (int i = 0; i < strs.Length; i++)
            {
                bytes[i] = Convert.ToByte(strs[i], 16);
            }

            return Encoding.ASCII.GetString(bytes);
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            RegistCtl registCtl = new RegistCtl();
            FrmSoftReg softReg = new FrmSoftReg();
            softReg.Show();
        }

        private void cbPermanent_CheckedChanged(object sender, EventArgs e)
        {
            dtp.Enabled = !cbPermanent.Checked;
        }
    }
}
